package com.hmdp.interceptor;/**
 * @Author 董熠涛
 * @Date 2025/4/14  21:38
 */

import com.hmdp.dto.UserDTO;
import com.hmdp.utils.UserHolder;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @Author:董熠涛
 * @ 2025/4/1421:38
 */
public class loginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        Object sessionUserKey = session.getAttribute("SESSION_USER_KEY");
        if (sessionUserKey == null){
            response.sendRedirect("/login.html");
            return false;
        }
        UserHolder.saveUser(((UserDTO)sessionUserKey));
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

        UserHolder.removeUser();
    }
}
